home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap07 / IdleDemo / IdleDemo.cpp next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  2.7 KB  |  126 lines

  1. //***********************************************************************
  2. //
  3. //  IdleDemo.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "Resource.h"
  9. #include "IdleDemo.h"
  10.  
  11. CMyApp myApp;
  12.  
  13. /////////////////////////////////////////////////////////////////////////
  14. // CMyApp member functions
  15.  
  16. BOOL CMyApp::InitInstance ()
  17. {
  18.     m_pMainWnd = new CMainWindow;
  19.     m_pMainWnd->ShowWindow (m_nCmdShow);
  20.     m_pMainWnd->UpdateWindow ();
  21.     return TRUE;
  22. }
  23.  
  24. BOOL CMyApp::OnIdle (LONG lCount)
  25. {
  26.     CWinApp::OnIdle (lCount);
  27.  
  28.     BOOL bReturn = TRUE;
  29.     if (lCount >= 2) {
  30.         CMainWindow* pWnd = (CMainWindow*) m_pMainWnd;
  31.         if (!pWnd->CursorInClient ()) {
  32.             pWnd->UpdateReadout ();
  33.             bReturn = FALSE;
  34.         }
  35.     }
  36.     return bReturn;
  37. }
  38.  
  39. /////////////////////////////////////////////////////////////////////////
  40. // CMainWindow message map and member functions
  41.  
  42. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  43.     ON_WM_PAINT ()
  44.     ON_WM_MOUSEMOVE ()
  45.     ON_COMMAND (IDM_EXIT, OnExit)
  46. END_MESSAGE_MAP ()
  47.  
  48. CMainWindow::CMainWindow ()
  49. {
  50.     Create (NULL, "IdleDemo", WS_OVERLAPPEDWINDOW, rectDefault,
  51.         NULL, MAKEINTRESOURCE (IDR_MAINFRAME));
  52. }
  53.  
  54. void CMainWindow::OnPaint ()
  55. {
  56.     CPaintDC dc (this);
  57.     UpdateReadout (&dc);
  58. }
  59.  
  60. void CMainWindow::OnMouseMove (UINT nFlags, CPoint point)
  61. {
  62.     UpdateReadout ();
  63. }
  64.  
  65. void CMainWindow::OnExit ()
  66. {
  67.     SendMessage (WM_CLOSE, 0, 0);
  68. }
  69.  
  70. BOOL CMainWindow::CursorInClient ()
  71. {
  72.     CRect rect;
  73.     GetClientRect (&rect);
  74.     ClientToScreen (&rect);
  75.  
  76.     POINT point;
  77.     ::GetCursorPos (&point);
  78.  
  79.     BOOL bReturn = FALSE;
  80.     if (rect.PtInRect (point) && (WindowFromPoint (point) == this))
  81.         bReturn = TRUE;
  82.  
  83.     return bReturn;
  84. }
  85.  
  86. void CMainWindow::UpdateReadout (CDC* pDC)
  87. {
  88.     BOOL bDeleteDC = FALSE;
  89.     if (pDC == NULL) {
  90.         pDC = new CClientDC (this);
  91.         bDeleteDC = TRUE;
  92.     }
  93.  
  94.     CRect rect;
  95.     GetClientRect (&rect);
  96.     int nWidth = rect.Width ();
  97.     int nHeight = rect.Height ();
  98.  
  99.     TEXTMETRIC tm;
  100.     pDC->GetTextMetrics (&tm);
  101.     int cx = tm.tmAveCharWidth;
  102.     int cy = tm.tmHeight;
  103.  
  104.     rect.left = (nWidth - (cx * 16)) / 2;
  105.     rect.right = rect.left + (cx * 16);
  106.     rect.top = (nHeight - (cy * 2)) / 2;
  107.     rect.bottom = rect.top + (cy * 2);
  108.  
  109.     pDC->Rectangle (rect);
  110.  
  111.     if (CursorInClient ()) {
  112.         POINT point;
  113.         ::GetCursorPos (&point);
  114.         ScreenToClient (&point);
  115.  
  116.         CString strReadout;
  117.         strReadout.Format ("%d, %d", point.x, point.y);
  118.  
  119.         pDC->DrawText (strReadout, &rect, DT_CENTER | DT_VCENTER |
  120.             DT_SINGLELINE);
  121.     }
  122.  
  123.     if (bDeleteDC)
  124.         delete pDC;
  125. }
  126.